import dtime from 'time-formater'

const generateRoutesFromMenu = (menuData = [], indexPaths = [], parentMenu, directPaths = []) => {
    // let routes = [];
    // for (let i = 0; i < menuData.length; i++) {
    //     const menu = menuData[i];
    //     let component = menu.component;
    //     let compName = (component && component.length > 0) ? component : "home/content";
    //     compName = compName.startsWith("/") ? compName.substring(1) : compName;

    //     try {
    //         menu['component'] = require('@/page/' + compName + '.vue').default;
    //     } catch (e) {
    //         console.log("error for require component: " + compName + ", errMsg: " + e);
    //         menu['component'] = require('@/page/home/content.vue').default;
    //     }

    //     // URL
    //     let path = menu.path;
    //     if (!path.startsWith("/")) {
    //         path = "/" + path;
    //     }
    //     menu.url = parentMenu ? (parentMenu.url + path) : path;
    //     let meta = {title: menu.name};
    //     menu.meta = meta;

    //     // 顶层如果没有下级菜单，则显示为直接菜单
    //     if ((!menu.children || menu.children.length <= 0)
    //         && !parentMenu) {
    //         menu.noDropdown = true; // 直接菜单
    //     }

    //     if (!menu.children || menu.children.length <= 0) {
    //         directPaths.push(menu.url);
    //     }

    //     routes.push(menu);

    //     if (menu.index) {
    //         indexPaths.push(menu.url);
    //     }

    //     menu.children = menu.children || [];

    //     if (!menu.noDropdown) {
    //         generateRoutesFromMenu(menu.children, indexPaths, menu, directPaths);
    //     }
    // }

    // return routes;
};

export default {
    /**
     * 存储localStorage
     */
    setStore: (name, content) => {
        if (!name) return;
        if (typeof content !== 'string') {
            content = JSON.stringify(content);
        }
        window.localStorage.setItem(name, content);
    },
    /**
     * 获取localStorage
     */
    getStore: name => {
        if (!name) return;
        let value = window.localStorage.getItem(name);
        if (value !== null) {
            try {
                value = JSON.parse(value);
            } catch (e) {
                console.log(e);
            }
        }
        return value;
    },
    /**
     * 删除localStorage
     */
    removeStore: name => {
        if (!name) return;
        window.localStorage.removeItem(name);
    },
    /**
     * 让整数自动保留2位小数
     */
    returnFloat: value => {
        value=Math.round(parseFloat(value)*100)/100;
        let xsd=value.toString().split(".");
        if(xsd.length==1){
            value=value.toString()+".00";
            return value;
        }
        if(xsd.length>1){
            if(xsd[1].length<2){
                value=value.toString()+"0";
            }
            return value;
        }
    },
    /**
     * @param {date} 标准时间格式:Fri Nov 17 2017 09:26:23 GMT+0800 (中国标准时间)
     * @param {type} 类型
     *   type == 1 ---> "yyyy-mm-dd hh:MM:ss.fff"
     *   type == 2 ---> "yyyymmddhhMMss"
     *   type == '' ---> "yyyy-mm-dd hh:MM:ss"
     */
    formatDate: (date, type) => {
        var year = date.getFullYear();//年
        var month = date.getMonth() + 1 < 10 ? "0" + (date.getMonth() + 1) : date.getMonth() + 1;//月
        var day = date.getDate() < 10 ? "0" + date.getDate() : date.getDate();//日
        var hour = date.getHours() < 10 ? "0" + date.getHours() : date.getHours();//时
        var minutes = date.getMinutes() < 10 ? "0" + date.getMinutes() : date.getMinutes();//分
        var seconds = date.getSeconds() < 10 ? "0" + date.getSeconds() : date.getSeconds();//秒
        var milliseconds = date.getMilliseconds() < 10 ? "0" + date.getMilliseconds() : date.getMilliseconds() //毫秒
        if (type == 1) {
            return year + "-" + month + "-" + day + " " + hour + ":" + minutes + ":" + seconds + "." + milliseconds;
        } else if (type == 2) {
            return year + "" + month + "" + day + "" + hour + "" + minutes + "" + seconds;
        } else if (type == 3) {
            return year + "-" + month + "-" + day;
        } else {
            return year + "-" + month + "-" + day + " " + hour + ":" + minutes + ":" + seconds;
        }
    },
    /**
     * 时间转换：20150101010101 --> '2015-01-01 01:01:01'
     */
    parseToDate: (timeValue) => {
        var result = "";
        var year = timeValue.substr(0, 4);
        var month = timeValue.substr(4, 2);
        var date = timeValue.substr(6, 2);
        var hour = timeValue.substr(8, 2);
        var minute = timeValue.substr(10, 2);
        var second = timeValue.substr(12, 2);
        result = year + "-" + month + "-" + date + " " + hour + ":" + minute + ":" + second;
        return result;
    },
    /**
     * 判断空值
     */
    isEmpty: (keys) => {
        if (typeof keys === "string") {
            keys = keys.replace(/"|&nbsp;|\\/g, '').replace(/(^\s*)|(\s*$)/g, "");
            if (keys == "" || keys == null || keys == "null" || keys === "undefined") {
                return true;
            } else {
                return false;
            }
        } else if (typeof keys === "undefined") {  // 未定义
            return true;
        } else if (typeof keys === "number") {
            return false;
        } else if (typeof keys === "boolean") {
            return false;
        } else if (typeof keys == "object") {
            if (JSON.stringify(keys) == "{}") {
                return true;
            } else if (keys == null) { // null
                return true;
            } else {
                return false;
            }
        }

        if (keys instanceof Array && keys.length == 0) {// 数组
            return true;
        }
    },
    /**
     * 返回两位的小数的字符串
     */
    toFixedNum: (num) => {
        const tonum = Number(num).toFixed(2);
        return tonum;
    },
    showMessage: () => {
        this.$message({
            showClose: true,
            message: '对不起，您暂无此操作权限~',
            type: 'success'
        });
    },
    /**
     * 读取base64
     */
    readFile: file => {
        console.log(file)
        //var file = this.files[0];
        //判断是否是图片类型
        if (!/image\/\w+/.test(file.raw.type)) {
            alert("只能选择图片");
            return false;
        }
        var reader = new FileReader();
        reader.readAsDataURL(file);
        reader.onload = function (e) {
            var filedata = {
                filename: file.name,
                filebase64: e.target.result
            }
            alert(filedata)
        }
    },
    /**
     * 需要递归循环children,重新赋值component
     */
    generateRoutesFromMenu,

    /**
     * 格式化日期
     *
     * @param dt
     * @returns {*}
     */
    formatDateTime(dt) {
        if (!dt) {
            return;
        }

        if (typeof dt == 'string') {
            dt = dt.replace(/-/g, "/");
        }

        return dtime(dt).format('YYYY-MM-DD HH:mm:ss');
    },
    formatDay(dt) {
        if (!dt) {
            return;
        }

        if (typeof dt == 'string') {
            dt = dt.replace(/-/g, "/");
        }

        return dtime(dt).format('YYYY-MM-DD');
    },
    formatTime(dt) {
        if (!dt) {
            return;
        }

        if (typeof dt == 'string') {
            dt = dt.replace(/-/g, "/");
        }

        return dtime(dt).format('HH:mm:ss');
    }
}

